package com.xmb.wechat.view.wechat.money;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;
import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.application.WechatStartApplication;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.bean.PayProofBean;
import com.xmb.wechat.definterface.CommonIAndSListener;
import com.xmb.wechat.definterface.HintListener;
import com.xmb.wechat.definterface.PicChooseWithOutCutCallBack;
import com.xmb.wechat.definterface.onBgChooseListener;
import com.xmb.wechat.delegate.PayProofDelegate;
import com.xmb.wechat.dialog.BGSelectDailog;
import com.xmb.wechat.helper.DataHelper;
import com.xmb.wechat.util.PicChooseUtils;
import com.xmb.wechat.util.PicLoadUtils;
import com.xmb.wechat.widget.TitleLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @Author lijiangop
 * @CreateTime 2020/4/15 08:50
 */
public class WechatFundPayProofActivity extends BaseActivity {
    private static final int REQUEST_CODE_ADD   = 107;
    private static final int REQUEST_CODE_IMAGE = 108;

    @BindView(R2.id.title_layout)
    TitleLayout mTitleLayout;
    @BindView(R2.id.iv_bg)
    ImageView   mIvBg;

    private PayProofDelegate   mDelegate;
    private BGSelectDailog     mBgSelectDialog;
    private String             mBg;

    public WechatFundPayProofActivity() {
        super(R.layout.activity_fund_pay_proof);
    }

    @Override
    protected void initView() {
        mDelegate = new PayProofDelegate(this);
        mTitleLayout.setRightClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDelegate.clearData();
                WechatStartApplication.getBoxStore().boxFor(PayProofBean.class).removeAll();
            }
        });
        showExemption(new HintListener() {
            @Override
            public void onConfirm() {
                //do nothing
            }

            @Override
            public void onCancel() {
                WechatFundPayProofActivity.this.finish();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        refresh();
    }

    private void refresh() {
        mDelegate.clearData();
        List<PayProofBean> data = WechatStartApplication.getBoxStore().boxFor(PayProofBean.class).query().build().find();
        mDelegate.addData(data);
    }

    @OnClick({R2.id.ll_bg, R2.id.btn_add, R2.id.btn_preview})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.ll_bg) {
            if (mBgSelectDialog == null) {
                mBgSelectDialog = new BGSelectDailog(WechatFundPayProofActivity.this, new onBgChooseListener() {
                    @Override
                    public void onDefaultChoose() {
                        mBg = null;
                        Picasso.get().load(R.drawable.img_default_gray).into(mIvBg);
                    }

                    @SuppressLint("IntentReset")
                    @Override
                    public void onUserDefinedChoose() {
                        PicChooseUtils.chooseOnePicWithOutCut(WechatFundPayProofActivity.this, new PicChooseWithOutCutCallBack() {
                            @Override
                            public void onPicChoose(String... Pic) {
                                if (TextUtils.isEmpty(Pic[0]))
                                    return;
                                mBg = Pic[0];
                                PicLoadUtils.loadSdcardPic(Pic[0], mIvBg);
                            }
                        });
                    }
                });
            }
            mBgSelectDialog.show();
        } else if (id == R.id.btn_add) {
            showListDialog(DataHelper.getPayProofTypeData(), new CommonIAndSListener() {
                @Override
                public void onChoose(int i, String s) {
                    WechatFundPayProofAddActivity.start(WechatFundPayProofActivity.this, i, REQUEST_CODE_ADD);
                }
            });
        } else if (id == R.id.btn_preview) {
            WechatFundPayProofDetailActivity.start(WechatFundPayProofActivity.this, mBg, (ArrayList<PayProofBean>) mDelegate.getData());
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_ADD)
            refresh();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mBgSelectDialog != null) {
            mBgSelectDialog.dismiss();
            mBgSelectDialog = null;
        }
    }
}
